package com.iphone.xkyy.web.controller;

import cn.iphone.xkyy.result.JSONResult;
import cn.iphone.xkyy.result.PageList;
import com.iphone.xkyy.input.MsgInput;
import com.iphone.xkyy.service.IMsginfoService;
import com.iphone.xkyy.domain.Msginfo;
import com.iphone.xkyy.query.MsginfoQuery;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/msginfo")
public class MsginfoController {

    @Autowired
    public IMsginfoService msginfoService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Msginfo msginfo){
        if(msginfo.getId()!=null){
            msginfoService.updateById(msginfo);
        }else{
            msginfoService.insert(msginfo);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        msginfoService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(msginfoService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(msginfoService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody MsginfoQuery query){
        Page<Msginfo> page = new Page<Msginfo>(query.getPage(),query.getRows());
        page = msginfoService.selectPage(page);
        return JSONResult.success(new PageList<Msginfo>(page.getTotal(),page.getRecords()));
    }

    /**
     * rabbitmq发消息
     */
    @ApiOperation(value = "发送消息,若发给多个用户，则传入多个msgInfo")
    @PostMapping("/sendMsg")
    public JSONResult sendMsg(@RequestBody List<MsgInput> list) {
        msginfoService.addTimingMsgInfo(list);
        return JSONResult.success();
    }
}
